package com.zy.baselib.widget

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.AttrRes
import androidx.annotation.Nullable
import androidx.core.content.ContextCompat
import com.zy.baselib.R
import com.zy.baselib.databinding.UiDefaultTitleBinding


/**
 * @author: shalei
 * @since: 2020-01-13 14:53
 * @Desc:
 **/
class TitleView(
    @Nullable context: Context,
    @Nullable attrs: AttributeSet?,
    @AttrRes defStyleAttr: Int
) : FrameLayout(context, attrs, defStyleAttr) {

    constructor(context: Context, attrs: AttributeSet?): this(context, attrs,0)

    constructor(context: Context): this(context, null)

    private var leftText: String? = null
    private var rightText: String? = null
    private var centerText: String? = null
    private var userBottomLine: Boolean = true
    private var mLeftListener: (()->Unit)? = null
    private var mRightListener: (()->Unit)? = null
    private var mTextColor = 0
    private var mLeftDrawable: Drawable? = null
    private var mRightDrawable: Drawable? = null
    private val mViewBinding: UiDefaultTitleBinding

    init {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.TitleView)
        leftText = ta.getString(R.styleable.TitleView_left_text)
        rightText = ta.getString(R.styleable.TitleView_right_text)
        centerText = ta.getString(R.styleable.TitleView_center_text)
        userBottomLine = ta.getBoolean(R.styleable.TitleView_bottom_view, true)
        mTextColor = ta.getColor(R.styleable.TitleView_text_color, ContextCompat.getColor(context, R.color.black_light))
        mLeftDrawable = ta.getDrawable(R.styleable.TitleView_left_src)
        mRightDrawable = ta.getDrawable(R.styleable.TitleView_right_src)
        ta.recycle()
        mViewBinding = UiDefaultTitleBinding.inflate(LayoutInflater.from(context),this, true)
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        mViewBinding.run {
            btnLeft.text = leftText
            btnRight.text = rightText
            textCenter.text = centerText
            bottomLine.visibility = if(userBottomLine) View.VISIBLE else View.GONE
            setTextColor(mTextColor)
            setLeftDrawable(mLeftDrawable)
            setRightDrawable(mRightDrawable)
            btnLeft.setOnClickListener {
                mLeftListener?.invoke()
            }

            btnRight.setOnClickListener {
                mRightListener?.invoke()
            }
        }
    }

    fun setLeftDrawable(drawable: Drawable?) {
        mViewBinding.btnLeft.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null)
    }

    fun setRightDrawable(drawable: Drawable?) {
        mViewBinding.btnRight.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, drawable, null)
    }

    /**
     * color的值
     */
    fun setTextColor(color: Int) {
        mViewBinding.run {
            btnLeft.setTextColor(color)
            btnRight.setTextColor(color)
            textCenter.setTextColor(color)
        }
    }

    fun setOnLeftClickListener(listener: (()->Unit)?) {
        this.mLeftListener = listener
    }

    fun setOnRightClickListener(listener: (() -> Unit)?) {
        this.mRightListener = listener
    }

    fun setTextCenterText(text: String?) {
        mViewBinding.textCenter.text = text
    }
}